/*
 * BCDB.java
 *
 * Created on 22 novembre 2006, 20:28
 *
 * Copyright (C) 2006-2007 Alexis ROBERT
 * Under the terms of the GPL2 license.
 */

package blbarc;
import java.net.MalformedURLException;
import java.util.Map;
import redstone.xmlrpc.*;

/**
 * BCDB client implementation
 * @author Alexis ROBERT
 */
public class BCDB {
    private String baseurl; // Web server URL
    private double protocolVersion = 1.1; // Wanted protocol version
    private XmlRpcClient rpcclient;
    
    /** Creates a new instance of BCDB */
    public BCDB() throws BCDBException {
        baseurl = Configuration.getInstance().getBCDBServer();
        try {
            this.rpcclient = new XmlRpcClient(this.baseurl, true);
            double serverVersion = (Double)(this.rpcclient.invoke("getProtocolVersion", new Object[] {}));
            if (serverVersion < protocolVersion) {
                throw new BCDBProtocolMismatch("Protocol version mismatch : "+serverVersion+" wanted "+this.protocolVersion);
            }
        } catch (MalformedURLException ex) {
            throw new BCDBException(ex.toString());
        } catch (XmlRpcException ex) {
            throw new BCDBException(ex.toString());
        } catch (XmlRpcFault ex) {
            throw new BCDBException(ex.toString());
        }
    }
    
    /**
     * Is the barcode exists in the BCDB database ?
     * @param barcodeTxt Barcode string
     * @return true if it exists, false otherwise
     */
    public boolean isBarcode(String barcodeTxt) {
        try {
            Object result = this.rpcclient.invoke("isBarcode", new Object[]{barcodeTxt});
            if (result.equals(1) == true)
                return true;
            else
                return false;
        } catch (XmlRpcException ex) {
            ex.printStackTrace();
        } catch (XmlRpcFault ex) {
            ex.printStackTrace();
        }   
        return false;
    }
    
    /**
     * Get the barcode title from the BCDB database
     * @param barcodeTxt Barcode string
     * @return Barcode title
     */
    public String getBarcodeTitle(String barcodeTxt) {
        Object result;
        try {
            result = this.rpcclient.invoke("getBarcodeTitle", new Object[] {barcodeTxt});
            return (String)result;
        } catch (XmlRpcException ex) {
            ex.printStackTrace();
        } catch (XmlRpcFault ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * Get the barcode object from the BCDB database
     * @param barcodeTxt Barcode string
     * @return Instancied barcode object with data
     */
    public Barcode getBarcode(String barcodeTxt) {
        Barcode finalobj = new Barcode();
        try {
            finalobj.title = (String)this.rpcclient.invoke("getBarcodeTitle", new Object[]{barcodeTxt});
            finalobj.fields = (Map<String,String>)this.rpcclient.invoke("getBarcodeFields", new Object[]{barcodeTxt});
        } catch (XmlRpcException ex) {
            ex.printStackTrace();
        } catch (XmlRpcFault ex) {
            ex.printStackTrace();
        }
        return finalobj;
    }
}
